package com.yoosora.common.mode.response;

import com.yoosora.common.except.CustomerException;
import com.yoosora.common.mode.IError;
import com.yoosora.common.mode.ToString;

/**
 * @author yoosora
 */
public abstract class AbstractResponse extends ToString {

	private static final long serialVersionUID = -8166661198562317575L;
	protected static final int SUCCESS_CODE = 0;
	protected Integer errcode;
	protected String errmsg;

	public AbstractResponse() {
	}

	public int getErrcode() {
		return this.errcode;
	}

	public String getErrmsg() {
		return this.errmsg;
	}

	public boolean isSuccess() {
		return this.errcode == 0;
	}

	public void check() {
		if (this.errcode != 0) {
			throw new CustomerException(this.errcode, this.errmsg);
		}
	}

	public static class Builder<T> {
		int errcode;
		String errmsg;

		public Builder() {
		}

		public Builder succ() {
			this.errcode = 0;
			return this;
		}

		public Builder failed(IError<Integer> err) {
			this.errcode = (Integer)err.errcode();
			this.errmsg = err.errmsg();
			return this;
		}

		public Builder failed(int errcode, String errmsg) {
			this.errcode = errcode;
			this.errmsg = errmsg;
			return this;
		}
	}
}
